<?php

/*
 *   this function returns an array describing a returned item
 *   it also manages the thumbnail caching
 */

include_once "sql.php";

function get_thumb( $url ){
  if( $data = file_get_contents( $url ) ){
    $fn = sha1($data);
    file_put_contents( $fn, $data );
    if( strlen($data) > 1 ){
      exec("convert " . $fn . " -resize 100x100^ -auto-level -gravity center -crop 100x100+0+0 " . $fn . ".jpg");
      $data = "data:image/jpg;base64," . base64_encode(file_get_contents($fn.".jpg")) ."";
      exec("rm " . $fn . ".jpg");
    }else $data = false;
    exec("rm " . $fn );
    return $data;
  }
  return false;
}

function get_item( $id ){
  $it = sqlQuery("SELECT * FROM kp_indexed_images WHERE id = $id;", array("id","image_link", "page_link", "title") );
  $it = $it[0];
  $t = get_thumb( $it["image_link"] );
  if($t != false) $it["data"] = $t;
  unset( $it[0] );
  return $it;
}


?>
